<?php
class AdsController extends AppController
{
    var $name='Ads';
    var $layout='admin';
    var $helpers=array('Javascript','Form','Time');
    var $components = array('Auth');
    function beforeFilter()
    {
        $this->userScope = array('admin_leve'=>1);
        $this->authorize = 'controller';
        $this->Auth->loginError = '未能登录成功,密码或帐号错误';
        $this->Auth->authError='还未登录或者你没有管理员权限';
    }
    
    function isAuthorized()
    {
           if($this->Auth->user('admin_leve')>0)
            return true;
        else
            return false;
    }
    function admin_index($time=null)
    {
         $now=date('Y-m-d');
        if($time == 'current'){
            $conditions=array('Ad.startTime <=' => $now,'Ad.endTime >=' => $now);
         }else if($time == 'passed'){
            $conditions=array('Ad.endTime <' => $now); 
        }else if($time == 'future'){
            $conditions=array('Ad.startTime >' => $now);
        }else{
            $conditions=array('1' => '1');
        }
        $adlist=$this->Ad->find('all',array('conditions' => $conditions,'limit'=>100,'order'=>'Ad.created desc'));
        $this->set('al',$adlist);
        $this->set('timetype',$time);
        $this->pageTitle='广告管理';
    }
    
    function admin_add()
    {
        if(!empty($this->data)){
            if($this->Ad->save($this->data)){
                   $this->flash('添加成功','index',5);
                   return;
            }
        }
    }
    
    function admin_edit($id)
    {
        if(empty($this->data)){
            if(empty($id) or !is_numeric($id)){
                $this->flash('请选择要编辑的广告','index',5);
                return;
             }
            $this->data=$this->Ad->read(null,$id);
            if(empty($this->data)){
                $this->flash('该广告数据并不存在','index',5);
                return;
            }
        }else{
            if($this->Ad->save($this->data))
                $this->flash('修改成功','index',5);
        }
    }
    function admin_del($id=null)
    {
        if(empty($id) or !is_numeric($id)){
            $this->flash('请选择要删除的广告','index',5);
            return;
        }
        if($this->Ad->del($id))
            $this->flash('删除成功','index',5);
         else
            $this->flash('未能删除成功','index',5);
    }
    
    function admin_delm($ids)
    {
        if(empty($ids) or !is_string($ids)){
            $this->flash('请选择要删除的广告','index',5);
            return;
        }
        $idarr=explode(',',$ids);
        if($this->Ad->deleteAll(array('id' => $idarr))){
            $this->flash('成功删除多个广告',"index",5);
            return;
        }
    }
}
?>